package mpcs.view
{
	import flash.events.Event;
	
	import mpcs.ApplicationFacade;
	import mpcs.NotiConst;
	import mpcs.model.vo.BasicInfoVO;
	import mpcs.model.vo.ContactInfoVO;
	import mpcs.model.vo.GlobalVO;
	import mpcs.utils.TrimUtils;
	import mpcs.view.components.UserInfoModule;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class UserInfoMediator extends Mediator {
		
		public static const NAME:String = "UserInfoMediator";
		
		private var tempBasicVO:BasicInfoVO;
		private var tempContactVO:ContactInfoVO;
		private var newPwd:String;
		private var object:Object;
		
		public function UserInfoMediator(viewComponent:UserInfoModule) {
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void {
			userInfoModule.basicInfo.addEventListener(userInfoModule.basicInfo.CLICK_SAVE_BASICINFO, onSaveBasicinfo);
			userInfoModule.contactInfo.addEventListener(userInfoModule.contactInfo.CLICK_SAVE_CONTACTINFO, onSaveContactInfo);
			userInfoModule.updatePwd.addEventListener(userInfoModule.updatePwd.CLICK_UPDATEPWD, onUpdatePwd);
			userInfoModule.setPosition.addEventListener(userInfoModule.setPosition.CLICK_SAVE_LATLNG, onSaveLatlng);
		}
		
		public function removeListeners():void {
			userInfoModule.basicInfo.removeEventListener(userInfoModule.basicInfo.CLICK_SAVE_BASICINFO, onSaveBasicinfo);
			userInfoModule.contactInfo.removeEventListener(userInfoModule.contactInfo.CLICK_SAVE_CONTACTINFO, onSaveContactInfo);
			userInfoModule.updatePwd.removeEventListener(userInfoModule.updatePwd.CLICK_UPDATEPWD, onUpdatePwd);
			trace("成功释放 UserInfoModule 中的监听器资源 ... ");
		}
		
		/**请求保存用户基本信息**/
		private function onSaveBasicinfo( event:Event ):void {
			// 封装修改过的用户Basicinfo
			tempBasicVO = new BasicInfoVO();
			tempBasicVO.setUserName(TrimUtils.Trim(userInfoModule.basicInfo.realName.text));
			tempBasicVO.setGender(TrimUtils.Trim(userInfoModule.basicInfo.gender.text));
			tempBasicVO.setBirthday(TrimUtils.Trim(userInfoModule.basicInfo.birthday.text));
			tempBasicVO.setBloodType(TrimUtils.Trim(userInfoModule.basicInfo.bloodType.text));
			tempBasicVO.setMarriage(TrimUtils.Trim(userInfoModule.basicInfo.marriage.text));
			tempBasicVO.setCareer(TrimUtils.Trim(userInfoModule.basicInfo.career.text));
			tempBasicVO.setEducation(TrimUtils.Trim(userInfoModule.basicInfo.education.text));
			tempBasicVO.setResidence(TrimUtils.Trim(userInfoModule.basicInfo.residence.text));
			tempBasicVO.setHome(TrimUtils.Trim(userInfoModule.basicInfo.hometown.text));
			tempBasicVO.setIdCard(TrimUtils.Trim(userInfoModule.basicInfo.icCard.text));		
			// 发送保存用户基本信息请求
			ApplicationFacade.getInstance().sendNotification( NotiConst.S_SAVE_BASICINFO, tempBasicVO );
		}
		
		/**请求保存用户联系信息**/
		private function onSaveContactInfo( event:Event ):void {
			// 封装修改过的用户ContactInfo
			tempContactVO = new ContactInfoVO();
			tempContactVO.setQQ(TrimUtils.Trim(userInfoModule.contactInfo.qq.text));
			tempContactVO.setMSN(TrimUtils.Trim(userInfoModule.contactInfo.msn.text));
			tempContactVO.setMobile(TrimUtils.Trim(userInfoModule.contactInfo.mobile.text));
			tempContactVO.setTel(TrimUtils.Trim(userInfoModule.contactInfo.telphone.text));
			tempContactVO.setZip(TrimUtils.Trim(userInfoModule.contactInfo.zip.text));
			// 发送保存用户联系信息请求
			ApplicationFacade.getInstance().sendNotification( NotiConst.S_SAVE_CONTACTINFO, tempContactVO );
		}
		
		/**请求修改密码**/
		private function onUpdatePwd( event:Event ):void {
			newPwd = TrimUtils.Trim(userInfoModule.updatePwd.renewPwd.text);
			// 发送修改密码请求
			ApplicationFacade.getInstance().sendNotification( NotiConst.S_UPDATEPWD, newPwd );
		}
		
		/**请求修改初始化位置**/
		private function onSaveLatlng( event:Event ):void {
			object = new Object();
			object.lat = TrimUtils.Trim(userInfoModule.setPosition.txt_lat.text);
			object.lng = TrimUtils.Trim(userInfoModule.setPosition.txt_lng.text);
			// 发送修改初始化位置请求
			ApplicationFacade.getInstance().sendNotification( NotiConst.S_SAVE_INIT_POSITION, object );
		}
		
		override public function listNotificationInterests():Array {
			return [
				NotiConst.CAN_S_BASICINFO,
				NotiConst.R_BASICINFO_SUCCESS,
				NotiConst.R_BASICINFO_FAILED,
				NotiConst.R_SAVE_BASICINFO_SUCCESS,
				NotiConst.R_SAVE_BASICINFO_FAILED,
				////////////////////////////////////////////////////////////////////////////////////////
				NotiConst.CAN_S_CONTACTINFO,
				NotiConst.R_CONTACTINFO_SUCCESS,
				NotiConst.R_CONTACTINFO_FAILED,
				NotiConst.R_SAVE_CONTACTINFO_SUCCESS,
				NotiConst.R_SAVE_CONTACTINFO_FAILED,
				////////////////////////////////////////////////////////////////////////////////////////
				NotiConst.R_UPDATEPWD_SUCCESS,
				NotiConst.R_UPDATEPWD_FAILED,
				////////////////////////////////////////////////////////////////////////////////////////
				NotiConst.CAN_S_MOBILELIST,
				NotiConst.R_MOBILELIST_SUCCESS,
				NotiConst.R_MOBILELIST_FAILED,
				////////////////////////////////////////////////////////////////////////////////////////
				NotiConst.R_SAVE_INIT_POSITION_SUCCESS,
				NotiConst.R_SAVE_INIT_POSITION_FAILED				
			];
		}
		
		override public function handleNotification( note:INotification ):void {
			switch ( note.getName() ) {
				case NotiConst.CAN_S_BASICINFO:
					// 请求用户基本信息
					trace(" 发送请求用户基本信息 ... handleNotification ... UserInfoMediator ... ");
					sendNotification(NotiConst.S_BASICINFO, GlobalVO.userVO);
					break;
				case NotiConst.R_BASICINFO_SUCCESS:
					// 成功返回用户基本信息,更新页面显示
					userInfoModule.basicInfo.basicvo = GlobalVO.userVO.getBasicInfoVO();
					userInfoModule.basicInfo.email.text = GlobalVO.userVO.getEmail();
					break;
				case NotiConst.R_BASICINFO_FAILED:
					// 请求用户基本信息失败
					Alert.show( "错误代码: " + note.getBody() , "返回用户基本信息失败");
					break;
				case NotiConst.R_SAVE_BASICINFO_SUCCESS:
					// 成功保存用户基本信息
					userInfoModule.basicInfo.returnTips.text = note.getBody() as String;
					userInfoModule.basicInfo.tipsColor = 0x4E8ECB;
					userInfoModule.basicInfo.returnTips.visible = true;
					userInfoModule.basicInfo.basicInfoSave.enabled = false;
					break;
				case NotiConst.R_SAVE_BASICINFO_FAILED:
					// 保存用户基本信息失败
					Alert.show( "错误代码: " + note.getBody() , "保存用户基本信息失败");
					userInfoModule.basicInfo.returnTips.text = note.getBody() as String;
					userInfoModule.basicInfo.tipsColor = 0xFF0000;
					userInfoModule.basicInfo.returnTips.visible = true;
					userInfoModule.basicInfo.basicInfoSave.enabled = true;
					break;
				
				////////////////////////////////////////////////////////////////////////////////////////
				
				case NotiConst.CAN_S_CONTACTINFO:
					// 请求用户联系信息
					trace(" 发送请求用户联系信息 ... handleNotification ... UserInfoMediator ... ");
					sendNotification(NotiConst.S_CONTACTINFO, GlobalVO.userVO);
					break;
				case NotiConst.R_CONTACTINFO_SUCCESS:
					// 成功返回用户联系信息,更新页面显示
					userInfoModule.contactInfo.contactvo = GlobalVO.userVO.getContactInfoVO();
					break;
				case NotiConst.R_CONTACTINFO_FAILED:
					// 请求用户联系信息失败
					Alert.show( "错误代码: " + note.getBody() , "返回用户联系信息失败");
					break;
				case NotiConst.R_SAVE_CONTACTINFO_SUCCESS:
					// 成功保存用户联系信息
					userInfoModule.contactInfo.returnTips.text = note.getBody() as String;
					userInfoModule.contactInfo.tipsColor = 0x4E8ECB;
					userInfoModule.contactInfo.returnTips.visible = true;
					userInfoModule.contactInfo.contactInfoSave.enabled = false;
					break;
				case NotiConst.R_SAVE_CONTACTINFO_FAILED:
					// 保存用户联系信息失败
					Alert.show( "错误代码: " + note.getBody() , "保存用户联系信息失败");
					userInfoModule.contactInfo.returnTips.text = note.getBody() as String;
					userInfoModule.contactInfo.tipsColor = 0xFF0000;
					userInfoModule.contactInfo.returnTips.visible = true;
					userInfoModule.contactInfo.contactInfoSave.enabled = true;
					break;
				
				////////////////////////////////////////////////////////////////////////////////////////
				
				case NotiConst.R_UPDATEPWD_SUCCESS:
					// 修改密码成功
					userInfoModule.updatePwd.returnTips.text = note.getBody() as String;
					userInfoModule.updatePwd.tipsColor = 0x4E8ECB;
					userInfoModule.updatePwd.returnTips.visible = true;
					userInfoModule.updatePwd.btn_updatePwd.enabled = false;
					break;
				case NotiConst.R_UPDATEPWD_FAILED:
					// 修改密码失败
					Alert.show( "错误代码: " + note.getBody() , "修改密码失败");
					userInfoModule.updatePwd.returnTips.text = note.getBody() as String;
					userInfoModule.updatePwd.tipsColor = 0xFF0000;
					userInfoModule.updatePwd.returnTips.visible = true;
					userInfoModule.updatePwd.btn_updatePwd.enabled = true;
					break;
				
				////////////////////////////////////////////////////////////////////////////////////////
				
				case NotiConst.CAN_S_MOBILELIST:
					trace("sendNotification(NotiConst.S_MOBILELIST) ... ");
					// 请求绑定手机信息
					sendNotification(NotiConst.S_MOBILELIST);
					break;
				case NotiConst.R_MOBILELIST_SUCCESS:
					// 请求绑定手机信息成功
					initData();
					break;
				case NotiConst.R_MOBILELIST_FAILED:
					// 请求绑定手机信息失败
					Alert.show( "错误代码: " + note.getBody() , "请求绑定手机信息失败");
					break;
				
				////////////////////////////////////////////////////////////////////////////////////////
				
				case NotiConst.R_SAVE_INIT_POSITION_SUCCESS:
					// 请求设定手机位置成功,更新显示
					userInfoModule.setPosition.tips.text = "设定手机初始位置成功";
					userInfoModule.setPosition.tipsColor = 0x4E8ECB;
					userInfoModule.setPosition.tips.visible = true;
					GlobalVO.userVO.getMobileList()[0].setLongitude(object.lng);
					GlobalVO.userVO.getMobileList()[0].setLatitude(object.lat);
					userInfoModule.mobileList.longitude.text = GlobalVO.userVO.getMobileList()[0].getLongitude();
					userInfoModule.mobileList.latitude.text = GlobalVO.userVO.getMobileList()[0].getLatitude();
					break;
				case NotiConst.R_SAVE_INIT_POSITION_FAILED:
					userInfoModule.setPosition.tipsColor = 0xFF0000;
					userInfoModule.setPosition.tips.visible = true;
					userInfoModule.setPosition.tips.text = "错误代码: " + note.getBody() , "请求设定手机位置失败";
					// 请求设定手机位置失败
					Alert.show( "错误代码: " + note.getBody() , "请求设定手机位置失败");
					break;
			}
		}
		
		public function initData():void{
			trace("初始化绑定手机信息 ... ");
			/*userInfoModule.mobileList.mobile.text = GlobalVO.userVO.getMobileVO().getMobile();
			userInfoModule.mobileList.relationship.text = GlobalVO.userVO.getMobileVO().getRelationship();
			userInfoModule.mobileList.status.text = GlobalVO.userVO.getMobileVO().getStatus();
			userInfoModule.mobileList.belongto.text = GlobalVO.userVO.getMobileVO().getBelongto();
			userInfoModule.mobileList.begintime.text = GlobalVO.userVO.getMobileVO().getBegintime();
			userInfoModule.mobileList.type.text = GlobalVO.userVO.getMobileVO().getType();
			userInfoModule.mobileList.longitude.text = GlobalVO.userVO.getMobileVO().getLongitude();
			userInfoModule.mobileList.latitude.text = GlobalVO.userVO.getMobileVO().getLatitude();
			userInfoModule.mobileList.radius.text = GlobalVO.userVO.getMobileVO().getRadius();*/
			var arr:Array = GlobalVO.userVO.getMobileList();
			if(arr.length == 0)
				return;
			for(var i:int = 0 ; i < arr.length ; i++){
				userInfoModule.mobileList.mobile.text = arr[i].getMobile();
				userInfoModule.mobileList.relationship.text = arr[i].getRelationship();
				userInfoModule.mobileList.status.text = arr[i].getStatus();
				userInfoModule.mobileList.belongto.text = arr[i].getBelongto();
				userInfoModule.mobileList.begintime.text = arr[i].getBegintime();
				userInfoModule.mobileList.type.text = arr[i].getType();
				userInfoModule.mobileList.longitude.text = arr[i].getLongitude();
				userInfoModule.mobileList.latitude.text = arr[i].getLatitude();
				userInfoModule.mobileList.radius.text = arr[i].getRadius();
			}
			
		}
		
		private function get userInfoModule ():UserInfoModule {
			return viewComponent as UserInfoModule;
		}
	}
}